package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.ICallContentService;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.HostCallContentMapper;
import com.ee.chat.common.entity.AdminUser;
import com.ee.chat.common.entity.HostCallContent;
import com.ee.chat.common.service.IPushMessageService;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Service
public class CallContentServiceImpl implements ICallContentService {
    @Resource
    HostCallContentMapper hostCallContentMapper;
    @Resource
    IOperationService operationService;
    @Resource
    IPushMessageService pushMessageService;

    @Override
    public Result list(HttpServletRequest request, Integer page, Integer limit, Integer userId, Integer status, String time) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(StringUtil.isEmpty(time) && userId == null && status == null){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/audit/callContent/viewList")){
                data.put("datas", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = hostCallContentMapper.list(p, userId, status, beginTime, endTime);

        data.put("datas", iPage.getRecords());
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "查询主播招呼列表列表数据");

        return Result.returnSuccess(data);
    }

    @Override
    public Result audit(HttpServletRequest request, Integer id, Integer status, String reason) throws Exception {
        HttpSession session = request.getSession(true);
        AdminUser adminUser = (AdminUser)session.getAttribute("adminUser");
        Integer userId = adminUser.getId();

        Map<String, String> push = new HashMap<>();
        HostCallContent hostCallContent = hostCallContentMapper.selectById(id);

        if(hostCallContent.getStatus() != 0){
            return Result.returnFailure("该记录已经审核，不能重复审核");
        }

        if(status == 1){    //通过
            push.put("title", "招呼用语审核通过");
            push.put("content", "您提交的”"+hostCallContent.getContent()+"“招呼用语审核已经通过");
        }else if(status == 2){  //拒绝
            hostCallContent.setReason(hostCallContent.getReason()==null ? reason : hostCallContent.getReason()+"\n"+reason);

            push.put("title", "基本信息更新审核驳回");
            push.put("content", "您提交的”"+hostCallContent.getContent()+"“招呼用语审核被驳回，原因：" + reason);
        }

        hostCallContent.setStatus(status);
        hostCallContent.setAuditTime(new Date());
        hostCallContent.setAuditUserId(userId);
        hostCallContentMapper.updateById(hostCallContent);

        //推送消息
        push.put("type", "3");
        pushMessageService.pushGeneralMessage(hostCallContent.getUserId(), push);

        return Result.returnSuccess(null);
    }

    @Override
    public Result cancelAudit(HttpServletRequest request, Integer id, String reason) throws Exception {
        HttpSession session = request.getSession(true);
        AdminUser adminUser = (AdminUser)session.getAttribute("adminUser");
        Integer userId = adminUser.getId();

        HostCallContent hostCallContent = hostCallContentMapper.selectById(id);

        if(hostCallContent.getStatus() == 0){
            return Result.returnFailure("该记录未审核，无法撤销审核");
        }

        hostCallContent.setStatus(0);
        hostCallContent.setAuditTime(new Date());
        hostCallContent.setAuditUserId(userId);
        hostCallContent.setReason(hostCallContent.getReason()+"\n"+reason);
        hostCallContentMapper.updateById(hostCallContent);

        operationService.addOperationLog(request, "撤销审核"+ hostCallContent.getUserId()+"用户设置的招呼用语"+ hostCallContent.getId());

        Map<String, String> push = new HashMap<>();

        push.put("title", "撤销基本信息更新审核");
        push.put("content", "您提交的”"+hostCallContent.getContent()+"“招呼用语审核已被撤销，请耐心等待重新审核，原因：" + reason);
        //推送消息
        push.put("type", "3");
        pushMessageService.pushGeneralMessage(hostCallContent.getUserId(), push);

        return Result.returnSuccess(null);
    }
}
